package org.example.demo123.controller;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import jakarta.servlet.http.HttpServletResponse;
import org.example.demo123.Bean.People;
import org.example.demo123.Bean.Person;
import org.springframework.http.HttpEntity;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@RestController
public class RequestComtroller {

    @RequestMapping(value = "/handle01")
    public String handle01(String username, String password, String cellphone, boolean  agreement){
        System.out.println("username:"+username);
        System.out.println("password:"+password);
        System.out.println("cellphone:"+cellphone);
        System.out.println("agreement:"+agreement);
        return "ok!!!";

        }
    @RequestMapping(value = "/handle02")
    public String handle02(@RequestParam("username") String un,
                           @RequestParam("password") String pwd,
                           @RequestParam("cellphone") String cp,
                           @RequestParam("agreement") boolean  ok){
        System.out.println("username:"+un);
        System.out.println("password:"+pwd);
        System.out.println("cellphone:"+cp);
        System.out.println("agreement:"+ok);
        return "ok!!!";
    }

    @RequestMapping(value = "/handle03")
    public String handle03(Person  person){
        System.out.println(person);
        return "ok!!!";
    }

    @RequestMapping(value = "/handle04")
    public String handle04(@RequestHeader("Host") String host){
        System.out.println(host);
        return "ok!!!";
    }

    @RequestMapping(value = "/handle05")
    public String handle05(@CookieValue("hehe") String hehe){
        System.out.println(hehe);
        return "ok!!!";
    }

    @RequestMapping(value = "/handle06")
    public String handle06(People people){
        System.out.println(people);
        return "ok!!!";
    }

    @RequestMapping(value = "/handle07")
    public String handle07(@RequestBody People people){
        System.out.println(people);
        return "ok!!!";
    }

    @RequestMapping(value = "/handle08")
    public String handle08(People people,
                           @RequestParam("headerImg") MultipartFile headerImg,
                           @RequestParam("lifeImg") MultipartFile lifeImg
                           ) throws IOException {
        //获得原始文件名
        String headerImgName = headerImg.getOriginalFilename();

        // 保存图片到本地
        headerImg.transferTo(new File("D:\\"+headerImgName));
        return "ok!!!";
    }

    @RequestMapping(value = "/handle09")
    public String handle09(HttpEntity<People> httpEntity){
        //获得请求头
        System.out.println("请求头" + httpEntity.getHeaders());
        //获得请求体
        System.out.println("请求体" + httpEntity.getBody());
        return "ok!!!";
    }

//    @RequestMapping(value = "/handle10")
//    public String handle10(HttpServletRequest request, HttpServletResponse  response) throws IOException {
//        //进行原始的操作
//        return "ok!!!";
//    }

    //会自动把返回的对象转化成JSON的格式
    @RequestMapping("/resp01")
    public People resp01(){
        People people = new People();
        people.setUsername("张三");
        people.setPassword("123456");
        people.setCellphone("123456789");
        people.setAgreement(true);
        people.setSex("男");
        people.setGrade("2021级");
        people.setHobby(new String[]{"看电影","看小说"});
        return people;
     }


    @RequestMapping("/download")
    public ResponseEntity<byte[]> download() throws Exception {
        FileInputStream fileInputStream = new FileInputStream("D:\\1.png");

        byte[] bytes = fileInputStream.readAllBytes();
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .contentLength(bytes.length)
                //内容处理方式
                .header("Content-Disposition", "attachment;filename=1.png")
                .body(bytes);
    }


}
